{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ef7d3e22",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/multObj.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, -1, (0,0,255), 1)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "47f0269c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 2792 , 1396.0\n",
      "1 : 576 , 343.50461411476135\n",
      "2 : 4 , 2.8284270763397217\n",
      "3 : 4 , 2.8284270763397217\n",
      "4 : 568 , 287.3137083053589\n",
      "5 : 76 , 39.65685415267944\n",
      "6 : 688 , 388.73506212234497\n",
      "7 : 270 , 160.2670258283615\n",
      "8 : 682 , 433.36961901187897\n",
      "9 : 1172 , 587.6568541526794\n",
      "10 : 66 , 39.21320307254791\n",
      "11 : 66 , 39.21320307254791\n",
      "12 : 66 , 39.21320307254791\n",
      "13 : 66 , 39.21320307254791\n",
      "14 : 66 , 39.21320307254791\n",
      "15 : 66 , 39.21320307254791\n",
      "16 : 66 , 39.21320307254791\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/multObj.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "# 40: little circle \n",
    "# 160: question\n",
    "SymbolPerimeter = 160\n",
    "offset = round(SymbolPerimeter*0.2)\n",
    "validIdx = []\n",
    "for idx, cnt in enumerate(contours):\n",
    "    perimeter = cv.arcLength(cnt,True)\n",
    "    print(idx, \":\", cnt.size, \",\", perimeter)\n",
    "    \n",
    "    minP = SymbolPerimeter - offset\n",
    "    maxP = SymbolPerimeter + offset\n",
    "    if perimeter > minP and perimeter < maxP:\n",
    "        validIdx.append(idx)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "\n",
    "for idx in validIdx:\n",
    "    cv.drawContours(imgBackGround, contours, idx, (0,0,255), 1)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2428f1f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 : 3784 , 1892.0\n",
      "1 : 8 , 5.656854152679443\n",
      "2 : 8 , 5.656854152679443\n",
      "3 : 8 , 5.656854152679443\n",
      "4 : 8 , 5.656854152679443\n",
      "5 : 12 , 7.656854152679443\n",
      "6 : 8 , 5.656854152679443\n",
      "7 : 1556 , 908.0630509853363\n",
      "8 : 1352 , 791.979790687561\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/circles.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "#area\n",
    "# 44700: circle \n",
    "# 11560: moon\n",
    "#perimeter\n",
    "# 910: circle\n",
    "# 790: moon\n",
    "SymbolArea = 11560\n",
    "offset = round(SymbolArea*0.2)\n",
    "validIdx = []\n",
    "for idx, cnt in enumerate(contours):\n",
    "    area = cv.contourArea(cnt)\n",
    "#     area = cv.arcLength(cnt,True)\n",
    "    print(idx, \":\", cnt.size, \",\", area)\n",
    "    \n",
    "    minP = SymbolArea - offset\n",
    "    maxP = SymbolArea + offset\n",
    "    if area > minP and area < maxP:\n",
    "        validIdx.append(idx)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "\n",
    "for idx in validIdx:\n",
    "    cv.drawContours(imgBackGround, contours, idx, (0,0,255), 1)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "0f6a401a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0, 321, 241)\n",
      "((143.0143585205078, 116.95769500732422), (26.155366897583008, 81.62832641601562), 18.758649826049805)\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "cnt = contours[1]\n",
    "x,y,w,h = cv.boundingRect(cnt)\n",
    "center,size,angle = cv.minAreaRect(cnt)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, 1, (255,255,255), -1)\n",
    "\n",
    "#draw straight box\n",
    "cv.rectangle(imgBackGround,(x,y),(x+w, y+h),(0,255,0),2)\n",
    "\n",
    "#draw rotated box\n",
    "box = cv.boxPoints((center,size,angle))\n",
    "box = np.int0(box)\n",
    "cv.drawContours(imgBackGround,[box],0,(0,0,255),2)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "e596e944",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY_INV\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "oneIdx = 0\n",
    "cnt = contours[oneIdx]\n",
    "x,y,w,h = cv.boundingRect(cnt)\n",
    "center,size,angle = cv.minAreaRect(cnt)\n",
    "\n",
    "#\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, oneIdx, (255,255,255), -1)\n",
    "\n",
    "#draw rotated box\n",
    "box = cv.boxPoints((center,size,angle))\n",
    "box = np.int0(box)\n",
    "cv.drawContours(imgBackGround,[box],0,(0,0,255),1)\n",
    "\n",
    "#prepare a drawable color image\n",
    "imgNew = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgNew, contours, oneIdx, (255,255,255), -1)\n",
    "cv.drawContours(imgNew, [box],0, (0,255,0),1)\n",
    "\n",
    "#rectify angle by warpAffine\n",
    "M = cv.getRotationMatrix2D(center, angle, scale=1)\n",
    "dst = cv.warpAffine(imgNew, M, (img.shape[1],img.shape[0]))\n",
    "\n",
    "cv.imshow(\"thresh\", thresh)\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.imshow(\"dst\", dst)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c307f5cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# draw all contours when object is black and background is white\n",
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, -1, (0,0,255), 2)\n",
    "\n",
    "cv.imshow(\"imgBackGround\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ab57e7d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
